package com.lyd.controller;

import java.util.List;
import java.util.Scanner;

import com.lyd.entity.FileEntity;
import com.lyd.service.FileService;
import com.lyd.service.impl.FileServiceImpl;

public class FileController {
	static Scanner scanner = new Scanner(System.in);
	static FileService fileService = new FileServiceImpl();
	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		while (true) {
			System.out.println("1.添加文件2.修改文件名3.删除4.查询");
			int mean = scanner.nextInt();
			if (mean == 1) {
				addFile();
			} else if (mean == 2) {
				updateFile();
			} else if (mean == 3) {
				deleteFile();
			} else if (mean == 4) {
				queryFile();
			}
		}
	}

	public static void addFile() {
		FileEntity file = new FileEntity();
		System.out.println("请输入要添加的父文件parentNodeCode");
		String parentNodeCode = sc.next();
		if (!fileService.isExists(parentNodeCode)) {
			System.out.println("文件parentNodeCode不存在");
			return;
		}
		System.out.println("请输入要添加的nodeName");
		String nodeName=sc.next();
		if(fileService.isExists(nodeName)) {
			System.out.println("nodeName以存在，请重新输入");
			return;
		}
		System.out.println("请输入文件nodeCode");
		String nodeCode = sc.next();
		System.out.println("请输入文件类型");
		String nodeType= sc.next();
		file.setNodeType(nodeType);
		file.setParentNodeCode(parentNodeCode);
		file.setNodeCode(nodeCode);
		file.setNodeName(nodeName);
		fileService.addFile(file);
	}

	public static void updateFile() {
		FileEntity file = new FileEntity();
		System.out.println("请输入要修改的文件nodeName");
		String update = sc.next();
		if (!fileService.isExists(update)) {
			System.out.println("要修改的文件nodeName不存在");
			return;
		}
		
		System.out.println("请输入新的nodeCode");
		String newNodeCode = sc.next();
		if(fileService.isExists(newNodeCode)) {
			System.out.println("nodeCode以存，在请重新输入");
		}
		file.setNodeCode(newNodeCode);
		file.setNodeName(update);
		fileService.updateFile(file);
	}

	public static void deleteFile() {
		FileEntity file = new FileEntity();
		System.out.println("请输入要删除 的文件nodeName");
		String deleteNodeName=sc.next();
		if(!fileService.isExists(deleteNodeName)) {
			System.out.println("要删除的文件nodeName不存在");
			return;
		}else {
			fileService.deleteFile(deleteNodeName);
			file.setNodeName(deleteNodeName);
		}
	
	}

	public static void queryFile() {
		System.out.println("请输入要查询的关键字");
		String nodeName=sc.next();
		List<FileEntity> list = fileService.listAll(nodeName);
        for(FileEntity node:list) {
//        	if(nodeName.equals(nodeName))
       	System.out.println(node.toString());
//       	   System.out.println(nodeName.indexOf(nodeName);
        }
	}
//	for(String detail : list)
//		if(detail.name.contains("张"))
//		System.out.println("detail.name");
}
